package com.springbootbase.springbootproject.mapper;

import com.springbootbase.springbootproject.pojo.RecommendList;
import com.springbootbase.springbootproject.pojo.Recommendationcontact;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
public interface RecommendationcontactMapper {

//    contact_status里 0表示发起 1表示接受

    @Insert("INSERT INTO RecommendationContact (student_id, teacher_id, student_contact_info, teacher_contact_info, student_message, teacher_message, initiator, contact_status, create_at)" +
            " VALUES (#{studentId}, #{teacherId}, #{studentContactInfo}, #{teacherContactInfo}, #{studentMessage}, #{teacherMessage}, #{initiator}, #{contactStatus}, #{createAt})")
    void sendInvite(Recommendationcontact recommendationcontact);

    @Update("update recommendList set  status=1 where studentId=#{studentId} and teacherId=#{teacherId}")
    void changeContactStatus(Long studentId,Long teacherId);


    @Select("SELECT * FROM RecommendationContact rc " +
            "WHERE ((rc.student_id = #{id} AND rc.initiator = 'teacher') " +
            "   OR (rc.teacher_id = #{id} AND rc.initiator = 'student'))")
    ArrayList<Recommendationcontact> getInviteList(Long id);


    @Select("SELECT * FROM RecommendationContact rc " +
            "WHERE (rc.student_id = #{id} AND rc.initiator = 'student') " +
            "   OR (rc.teacher_id = #{id} AND rc.initiator = 'teacher') ")
    ArrayList<Recommendationcontact> getSendList(Long id);


    @Update("UPDATE RecommendationContact SET teacher_contact_info=#{accept_info},teacher_message=#{accept_message},contact_status=1,finish_at=now()  WHERE contact_id = #{contactId} AND initiator='student'")
    void acceptStudentContact(Long contactId,String accept_info,String accept_message);

    @Update("UPDATE RecommendationContact SET student_contact_info=#{accept_info},student_message=#{accept_message},contact_status=1,finish_at=now()  WHERE contact_id = #{contactId} AND initiator='teacher'")
    void acceptTeacherContact(Long contactId,String accept_info,String accept_message);


    @Select("SELECT * FROM RecommendationContact rc " +
            "WHERE (rc.student_id = #{id}  AND rc.contact_status=1) " +
            "   OR (rc.teacher_id = #{id}  AND rc.contact_status=1)")
    ArrayList<Recommendationcontact> getAllAcceptContact(Long id);


    @Select("select * from recommendationcontact where exists(select 1 from user where user.id=#{id} and userType='admin') ")
    ArrayList<Recommendationcontact> getList(Long id);

    @Select("select * from recommendationcontact")
    ArrayList<Recommendationcontact> getListForRecommend();

    @Update("update recommendationcontact set contact_status=2 where student_id=#{studentId} and teacher_id=#{teacherId}")
    void refuseContact(Long studentId, Long teacherId);

    @Delete("delete from recommendationcontact where contact_id=#{contactId}")
    void withdrawContact(Long contactId);

    @Select("select * from recommendationcontact where contact_id=#{contactId} ")
    Recommendationcontact getContact(Long contactId);

    @Select("select * from recommendList where studentId=#{studentId} and teacherId=#{teacherId}")
    RecommendList getRecommend(Long studentId,Long teacherId);

    @Update("update recommendList set status=0 where studentId=#{studentId} and teacherId=#{teacherId}")
    void updateRecommend(Long studentId,Long teacherId);

    @Select("select * from recommendationcontact where student_id=#{id}")
    ArrayList<Recommendationcontact> allContact(Long id);

    @Select("select count(*) from recommendationcontact where student_id=#{id} and initiator='student'")
    int countStudentContact(Long id);

    @Select("select count(*) from recommendationcontact where teacher_id=#{teacherId} and student_id=#{studentId}")
    int countContact(Long studentId,Long teacherId);

}
